package com.adfframeworks.labs.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.adfframeworks.labs.dao.CommunityMembersDAO;
import com.adfframeworks.labs.domain.Community;
import com.adfframeworks.labs.domain.Employee;
import com.adfframeworks.labs.service.CommunityMembersService;

@Service("CommunityMembersService")
public class CommunityMembersServiceImpl implements CommunityMembersService {

	private CommunityMembersDAO communityMembersDAO;
	private TransactionTemplate txTemplate;

	@Autowired
	@Qualifier("communityMembersDAOImpl")
	public void setCommunityMembersDAO(CommunityMembersDAO communityMembersDAO) {
		this.communityMembersDAO = communityMembersDAO;
	}

	@Autowired
	public void setTxTemplate(TransactionTemplate txTemplate) {
		this.txTemplate = txTemplate;
	}

	@Override
	public void create(final Integer community, final Integer employee) {
		txTemplate.execute(new TransactionCallback<Void>() {
			public Void doInTransaction(TransactionStatus txStatus) {
				try {
					communityMembersDAO.create(community, employee);
				} catch (RuntimeException e) {
					txStatus.setRollbackOnly();
					throw e;
				}
				return null;
			}
		});
	}

	@Override
	public List<Community> listCommunitiesOfEmployee(Integer employee) {
		return communityMembersDAO.listCommunitiesOfEmployee(employee);
	}

	@Override
	public List<Community> listCommunitiesAsOwner(Integer employee) {
		return communityMembersDAO.listCommunitiesAsOwner(employee);
	}

	@Override
	public List<Community> listCommunitiesAsNotOwner(Integer employee) {
		return communityMembersDAO.listCommunitiesAsNotOwner(employee);
	}

	@Override
	public List<Employee> listEmployeesFromCommunity(Integer community) {
		return communityMembersDAO.listEmployeesFromCommunity(community);
	}

	@Override
	public void delete(Integer community, Integer employee) {
		communityMembersDAO.delete(community, employee);
	}

	@Override
	public void deleteCommunity(Integer community) {
		communityMembersDAO.deleteCommunity(community);
	}

	@Override
	public void deleteEmployee(Integer employee) {
		communityMembersDAO.deleteEmployee(employee);
	}

}
